package com.neudu.glsb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.glsb.commons.JSONReturn;
import com.neudu.glsb.pojo.Employees;
import com.neudu.glsb.service.EmployeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class EmployeesController {

    @Autowired
    private EmployeesService employeesService;

    @Autowired
    private JSONReturn jsonReturn;

    /**
     * 获取所有员工信息，支持分页
     */
    @PostMapping("get_all_employees")
    public String getAll(@RequestBody Map<String, Object> pageInfoMap) {
        try {
            Integer pageNum = (Integer) pageInfoMap.get("pageNum");
            Integer pageSize = (Integer) pageInfoMap.get("pageSize");
            PageHelper.startPage(pageNum, pageSize);
            List<Employees> list = employeesService.list();
            PageInfo<Employees> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 根据条件查询员工信息，支持员工姓名模糊查询，支持分页
     */
    @PostMapping("get_employee_by")
    public String getByCondition(@RequestBody Map<String, Object> conditionMap) {
        try {
            Integer pageNum = (Integer) conditionMap.get("pageNum");
            Integer pageSize = (Integer) conditionMap.get("pageSize");
            String employeeName = (String) conditionMap.get("employeeName");

            PageHelper.startPage(pageNum, pageSize);
            LambdaQueryWrapper<Employees> queryWrapper = new LambdaQueryWrapper<>();
            if (!ObjectUtils.isEmpty(employeeName)) {
                queryWrapper.like(Employees::getEmployeeName, employeeName);
            }

            List<Employees> employees = employeesService.list(queryWrapper);
            PageInfo<Employees> pageInfo = new PageInfo<>(employees);
            return jsonReturn.returnSuccess(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 新增员工信息
     */
    @PostMapping("add_one_employee")
    public String addOne(@RequestBody Employees employee) {
        try {
            employeesService.save(employee);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 根据 ID 获取员工信息
     */
    @PostMapping("getemployeeby_id")
    public String getById(@RequestBody Map<String, Integer> idMap) {
        try {
            Integer id = idMap.get("id");
            if (ObjectUtils.isEmpty(id)) {
                return jsonReturn.returnError("ID不能为空");
            }
            Employees employee = employeesService.getById(id);
            return jsonReturn.returnSuccess(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 根据 ID 修改员工信息
     */
    @PostMapping("modifyemployeeby_id")
    public String modifyById(@RequestBody Employees employee) {
        try {
            if (ObjectUtils.isEmpty(employee.getId())) {
                return jsonReturn.returnError("更新失败，id 不能为空");
            }
            boolean success = employeesService.updateById(employee);
            if (success) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("更新失败，未找到对应的员工信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 根据 ID 删除员工信息
     */
    @PostMapping("removeemployee_by_id")
    public String removeById(@RequestBody Map<String, Integer> idMap) {
        try {
            Integer id = idMap.get("id");
            if (ObjectUtils.isEmpty(id)) {
                return jsonReturn.returnError("ID不能为空");
            }
            boolean success = employeesService.removeById(id);
            if (success) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("删除失败，未找到对应的员工信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}